package cn.le.crawler;

import cn.hutool.core.date.DateUtil;
import cn.le.annotation.CrawlerName;
import cn.le.entity.CrawlerHome;
import cn.le.entity.ZgzcqswData;
import cn.le.enums.EnumCrawler;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.zip.GZIPInputStream;

/**
 * 中国资产起诉网
 * www.rmfysszc.gov.cn
 */
@Component
@CrawlerName(value = EnumCrawler.Zgzcqsw)
public class ZGZCQSWCrawler extends AbstractCrawler implements Crawler{

    private static final String ITEM_URL = "https://www.rmfysszc.gov.cn/statichtml/rm_obj/";

    public static void main(String[] args) throws Exception {
        //6月5日10:00
        String a = "2020年 1月6日9:44";
        int index = a.indexOf("年");
        StringBuilder sb = new StringBuilder();
        if (index > -1){
            a = a.substring(index + 1,a.indexOf("日"));
            String[] s = a.split("月");
            for (int i = 0; i < s.length; i++) {
                int yue = Integer.valueOf(s[i].trim());
                if (yue > 9){
                    sb.append(yue);
                }else {
                    sb.append("0").append(yue);
                }
                if (i < s.length - 1){
                    sb.append("-");
                }
            }
        }
        System.out.println(sb);

        //ZGZCQSWCrawler z = new ZGZCQSWCrawler();
        //z.crawlerHome();
    }

    @Override
    public List<CrawlerHome> crawlerHome() throws Exception {
        JSONObject jsonObject = getJsonResult(1);
        int pageSum = parsingPageSum(jsonObject);
        List<ZgzcqswData> zgzcqswDataList = parsingData(jsonObject);
        for (int i = 2; i <= pageSum; i++) {
            jsonObject = getJsonResult(i);
            List<ZgzcqswData> list = parsingData(jsonObject);
            zgzcqswDataList.addAll(list);
        }
        System.out.println(zgzcqswDataList.size());

        return null;
    }

    private static List<ZgzcqswData> parsingData(JSONObject jsonObject){
        String html = (String) jsonObject.get("html");
        Document document = parseDocument(html);
        List<ZgzcqswData> dataList = new LinkedList<>();
        Elements contextElement = document.select("div.product");
        for (Element context : contextElement) {
            ZgzcqswData data = new ZgzcqswData();
            data.setHomeId(Long.parseLong(context.attr("id")));
            Elements elements = context.select("div.p_img").select("a");
            String title = elements.get(0).attr("title");
            data.setTitle(title);
            String itemUrl = elements.get(1).attr("href");
            itemUrl = itemUrl.replace("Handle/","");
            data.setItemUrl(ITEM_URL + itemUrl);
            //
            elements = context.select("div.prod-guj").select("p");
            if (elements.size() == 2){
                //评估价
                String consultPriceStr = elements.get(0).select("span").text();
                consultPriceStr = consultPriceStr.replace("万","");
                data.setConsultPrice(Double.valueOf(consultPriceStr) * 10000);
                //结束时间
                String endTimeStr = elements.get(1).select("span").text();
                endTimeStr = endTimeStr.replace("年 ","-").replace("月","-").replace("日"," ");
                data.setEnd(DateUtil.parse(endTimeStr));
                //状态
                String status =  context.select("div.prod-alink").select("a").text();
                data.setStatus(status);
            }else {
                //起拍价
                String initialPriceStr = elements.get(0).select("span").text();
                initialPriceStr = initialPriceStr.replace("万", "");
                data.setInitialPrice(Double.valueOf(initialPriceStr) * 10000);
                //评估价
                String consultPriceStr = elements.get(1).select("span").text();
                consultPriceStr = consultPriceStr.replace("万", "");
                data.setConsultPrice(Double.valueOf(consultPriceStr) * 10000);
                //开始时间
                String startTimeStr = elements.get(2).select("span").text();
                startTimeStr = startTimeStr.replace("月", "-").replace("日", " ");
                Date date = DateUtil.date();
                //获得年的部分
                int year = DateUtil.year(date);
                startTimeStr = year + "-" + startTimeStr;
                date = DateUtil.parse(startTimeStr);
                data.setStart(date);
                //状态
                String status =  elements.get(2).select("s").text();
                data.setStatus(status);
            }

            dataList.add(data);
        }
        return dataList;
    }



    private static int parsingPageSum(JSONObject jsonObject){
        String html = (String) jsonObject.get("page");
        Document document = parseDocument(html);
        Element element = document.select("body").select("a").last();
        String page = element.attr("onclick");
        page = page.replace("post(","").replace(")","");
        return Integer.valueOf(page);
    }


    private static JSONObject getJsonResult(int page){
        String result= "";
        try {
            URL realURL = new URL("https://www1.rmfysszc.gov.cn/ProjectHandle.shtml");
            HttpURLConnection conn = (HttpURLConnection) realURL.openConnection();
            conn.setRequestMethod("POST");
            conn.setUseCaches(false);
            conn.setDoInput(true);
            conn.setDoOutput(true);

            conn.setRequestProperty("Accept", "application/json, text/javascript, */*; q=0.01");
            conn.setRequestProperty("Accept-Encoding", "gzip, deflate, br");
            conn.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8");
            conn.setRequestProperty("Connection","keep-alive");
            conn.setRequestProperty("Content-Length","160");
            conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
            conn.setRequestProperty("Cookie","__jsluid_s=156479c91bcc8c77ca98f20a6bb3c653; ASP.NET_SessionId=u3tgcwoidlfezujf3lknphtz; Cookies-01=59063098");
            conn.setRequestProperty("Host","www1.rmfysszc.gov.cn");
            conn.setRequestProperty("Origin","https://www1.rmfysszc.gov.cn");
            conn.setRequestProperty("Referer","https://www1.rmfysszc.gov.cn/projects.shtml?dh=3&gpstate=1&wsbm_slt=1");
            conn.setRequestProperty("Sec-Fetch-Dest","empty");
            conn.setRequestProperty("Sec-Fetch-Mode","cors");
            conn.setRequestProperty("Sec-Fetch-Site","same-origin");
            conn.setRequestProperty("X-Requested-With","XMLHttpRequest");
            conn.setRequestProperty("user-agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36");
            conn.connect();
            //参数
            DataOutputStream out = new DataOutputStream(conn
                    .getOutputStream());
            String pram = "type=1&name=&area=&state=0&time=0&time1=&time2=&money=&money1=&number=0&fid1=&fid2=&fid3=&order=2&include=0&page="+page;
            out.writeBytes(pram);
            //获取返回值并解压缩
            GZIPInputStream gzin = new GZIPInputStream(conn.getInputStream());
            InputStreamReader isr = new InputStreamReader(gzin,"utf-8");
            BufferedReader bufferedReader = new BufferedReader(isr);
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                result += line;
            }
            JSONObject jsonObject = JSONObject.parseObject(result);
            return jsonObject;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
